﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using 第十次作业学生管理.Data;
using 第十次作业学生管理.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace 第十次作业学生管理.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SchoolsController : ControllerBase
    {
        private readonly SchoolContext _context;
        private readonly LogHelper _logHelper;

        public SchoolsController(SchoolContext context)
        {
            _context = context;
            _logHelper = new LogHelper(context);
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<School>>> GetSchools()
        {
            await _logHelper.AddLogAsync("Retrieved all schools");
            return await _context.Schools.ToListAsync();
        }

        [HttpPost]
        public async Task<ActionResult<School>> AddSchool(School school)
        {
            _context.Schools.Add(school);
            await _context.SaveChangesAsync();
            await _logHelper.AddLogAsync($"Added school: {school.Name}");
            return CreatedAtAction(nameof(GetSchools), new { id = school.SchoolId }, school);
        }
    }

}
